package com.yang.mall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.server.ServerWebExchange;

@Configuration
public class CorsConfig{

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //  1.配置跨域规则
        corsConfiguration.addAllowedOriginPattern("*"); // 允许任何主机来源
        corsConfiguration.addAllowedHeader("*");    //  允许任何请求头
        corsConfiguration.addAllowedMethod("*");    //  允许任何请求方式
        corsConfiguration.setAllowCredentials(true);//  允许携带cookie跨域

        //  2.对于任何路径都应用当前跨域规则
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
